package com.game.service.imp;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.game.mapper.RoomMapper;
import com.game.mapper.UserMapper;
import com.game.modal.Room;
import com.game.service.IRoomService;
import com.game.util.ResultEnum;
import com.game.util.ResultInfo;

@Service
public class RoomService implements IRoomService{

	@Autowired
	private RoomMapper roomMapper;
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public ResultInfo create(String uid) {
		Room room = new Room();
		room.setRoomId(UUID.randomUUID().toString());
		ResultInfo result = roomMapper.insert(room)>0?ResultInfo.SUCCESS:ResultInfo.ERROR;
		result.setResult(room.getRoomId());
		if(ResultInfo.SUCCESS.equals(result)){
			//result = join(uid,room.getRoomId());
			result.setResult(room.getRoomId());
		}
		return result;
	}

	@Override
	public ResultInfo join(String uid,String roomNo){
		
		int count = roomMapper.selectBy(roomNo);
	
		if(count>0){
			if(userMapper.hasExsistUser(uid, roomNo)==0){
				int c = userMapper.insertUserRoom(uid,roomNo);
				if(c!=1){
					throw new RuntimeException("添加用户房间失败");
				}
			}
			return ResultInfo.SUCCESS;
		}else{
			return new ResultInfo(ResultEnum.NOTEXSISTDATA);
		}
	}
}
